package com.kkkkkba.search.manager;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kkkkkba.search.dataSource.*;
import com.kkkkkba.search.model.dto.post.PostQueryRequest;
import com.kkkkkba.search.model.dto.search.SearchRequest;
import com.kkkkkba.search.model.dto.user.UserQueryRequest;
import com.kkkkkba.search.model.entity.Music;
import com.kkkkkba.search.model.entity.Video;
import com.kkkkkba.search.model.enums.SearchTypeEnum;
import com.kkkkkba.search.model.vo.*;
import com.kkkkkba.search.model.entity.Picture;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@Component
public class SearchFacade {

    @Resource
    private PostDataSource postDataSource;

    @Resource
    private UserDataSource userDataSource;

    @Resource
    private PictureDataSource pictureDataSource;

    @Resource
    private VideoDataSource videoDataSource;

    @Resource
    private CodeDataSource codeDataSource;

    @Resource
    private MusicDataSource musicDataSource;

    @Resource
    private DataSourseRegistry dataSourseRegistry;

    /**
     * 聚合搜索
     * @param searchRequest
     * @param request
     * @return
     */
    public SearchVO doSearchAll(@RequestBody SearchRequest searchRequest, HttpServletRequest request) {
        String type = searchRequest.getType();
        String searchText = searchRequest.getSearchText();
        SearchTypeEnum searchTypeEnum = SearchTypeEnum.getEnumByValue(type);
        long current = searchRequest.getCurrent();
        long pageSize = searchRequest.getPageSize();

        SearchVO searchVO = new SearchVO();
        if (searchTypeEnum == null) {
            Page<UserVO> userPage = userDataSource.doSearch(searchText,current,pageSize);

            Page<PostVO> postPage = postDataSource.doSearch(searchText,current,pageSize);

            Page<CodeVO> codePage = codeDataSource.doSearch(searchText,current,pageSize);

            Page<Picture> picturePage = pictureDataSource.doSearch(searchText,current,pageSize);

            Page<Video> videoPage = videoDataSource.doSearch(searchText,current,pageSize);

            Page<Music> musicPage = musicDataSource.doSearch(searchText,current,pageSize);

            searchVO.setUserList(userPage.getRecords());
            searchVO.setPostList(postPage.getRecords());
            searchVO.setPictureList(picturePage.getRecords());
            searchVO.setVideoList(videoPage.getRecords());
            searchVO.setCodeList(codePage.getRecords());
            searchVO.setMusicList(musicPage.getRecords());
        }
        else
        {
            DataSource dataSource = dataSourseRegistry.getDataSourceByType(type);
            Page page = dataSource.doSearch(searchText, current, pageSize);
            searchVO.setObjectList(page.getRecords());
        }
        return searchVO;
    }

    /**
     * 聚合搜索 分页
     * @param searchRequest
     * @param request
     * @return
     */
    public SearchPageVo doSearchByPage(SearchRequest searchRequest, HttpServletRequest request) {
        String type = searchRequest.getType();
        String searchText = searchRequest.getSearchText();
        SearchTypeEnum searchTypeEnum = SearchTypeEnum.getEnumByValue(type);
        long current = searchRequest.getCurrent();
        long pageSize = searchRequest.getPageSize();

        SearchPageVo searchVO = new SearchPageVo();
        if (searchTypeEnum == null) {
            Page<UserVO> userPage = userDataSource.doSearch(searchText,current,pageSize);

            Page<PostVO> postPage = postDataSource.doSearch(searchText,current,pageSize);

            Page<CodeVO> codePage = codeDataSource.doSearch(searchText,current,pageSize);

            Page<Picture> picturePage = pictureDataSource.doSearch(searchText,current,pageSize);

            Page<Video> videoPage = videoDataSource.doSearch(searchText,current,pageSize);

            Page<Music> musicPage = musicDataSource.doSearch(searchText,current,pageSize);
            searchVO.setUserPage(userPage);
            searchVO.setPostPage(postPage);
            searchVO.setPicturePage(picturePage);
            searchVO.setVideoPage(videoPage);
            searchVO.setCodePage(codePage);
            searchVO.setMusicPage(musicPage);
        }
        else{
            DataSource dataSource = dataSourseRegistry.getDataSourceByType(type);
            Page page = dataSource.doSearch(searchText, current, pageSize);
            searchVO.setObjectPage(page);
        }
        return searchVO;
    }
}
